//
//  BZWelcomeViewController.swift
//  weibo
//
//  Created by zzx on 2017/7/30.
//  Copyright © 2017年 zzx. All rights reserved.
//

import UIKit
import SnapKit
import SDWebImage

class BZWelcomeViewController: UIViewController {

    
    //MARK:- 懒加载
    fileprivate lazy var bgimageView: UIImageView = UIImageView(image: UIImage(named: "ad_background"))
    
    //用户头像
    fileprivate lazy var userImageView: UIImageView = {
        
        let imageView = UIImageView(image: UIImage(named: "avatar_default_big"))
        
        if let imageUrl = BZUserAccountViewModel.sharedViewModel.userAccount?.avatar_large {
            
            imageView.sd_setImage(with:URL(string: imageUrl) , placeholderImage: UIImage(named: "avatar_default_big"))
            
        }
        
        //设置圆角
        imageView.layer.cornerRadius = 45
        
        //截取多余部分
        imageView.layer.masksToBounds = true
        
        return imageView
        
    }()
    
    //消息内容
    fileprivate lazy var messageLabel: UILabel = {
        
        let label = UILabel()
        
        if let name = BZUserAccountViewModel.sharedViewModel.userAccount?.name {
            
            label.text = "欢迎回来,\(name)"
            
            label.alpha = 0
            
        }else{
            
            label.text = "欢迎回来"
            
        }
        //设置字体大小
        label.font = UIFont.systemFont(ofSize: 12)
        
        //文字颜色
        label.textColor = #colorLiteral(red: 0.6000000238, green: 0.6000000238, blue: 0.6000000238, alpha: 1)
        
        return label
    }()
    
    //视图显示时
    override func viewDidAppear(_ animated: Bool) {
        super.viewDidAppear(animated)
        
        startSpringAnimation()
        
    }
    
    //自定义视图
    override func loadView() {
        view = bgimageView
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()

        setupUI()
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    
    func setupUI() {
        
//        添加控件设置约束
        view.addSubview(userImageView)
        
        view.addSubview(messageLabel)
        
        userImageView.snp.makeConstraints { (make) in
            
            make.top.equalTo(view).offset(200)
            
            make.centerX.equalTo(view)
            
            make.size.equalTo(CGSize(width: 90, height: 90))
            
        }
        
        messageLabel.snp.makeConstraints { (make) in
            
            make.top.equalTo(userImageView.snp.bottom).offset(10)
            
            make.centerX.equalTo(userImageView)
            
        }
        
    }
    
    
    //MARK:- 开启弹簧效果动画
    private func startSpringAnimation(){
        
        userImageView.snp.updateConstraints { (make) in
            
            make.top.equalTo(view).offset(100)
            
        }
        
        UIView.animate(withDuration: 1, delay: 0, usingSpringWithDamping: 0.7, initialSpringVelocity: 10, options: [], animations: { 
            self.view.layoutIfNeeded()
        }) { (_) in
            
            UIView.animate(withDuration: 1, animations: { 
                self.messageLabel.alpha = 1.0
            }, completion: { (_) in
                
                NotificationCenter.default.post(name: Notification.Name(SwitchRootVCNotificationName), object: nil)
                
            })
            
            
        }
        
    }
    

}
